package com.company.project.controller;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.OrderComment;
import com.company.project.model.OrderMain;
import com.company.project.model.request.QueryParams;
import com.company.project.service.AdminUserService;
import com.company.project.service.OrderMainService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
* Created by CodeGenerator on 2020/04/21.
*/
@RestController
@RequestMapping("/order")
public class OrderMainController {
    @Resource
    private OrderMainService orderMainService;
    @Autowired
    private AdminUserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/generateOrder")
    public Result generateOrder(@RequestBody OrderMain orderMain) {
        if(orderMain.getFoodList() == null || orderMain.getUser() == null || orderMain.getFoodList().size() == 0) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return orderMainService.generateOrder(orderMain);
    }

    /**
     * 修改用户配送地址和电话
     */
    @PostMapping("/orderMainEdit")
    public Result orderMainEdit(@RequestBody OrderMain orderMain) {
        if(orderMain == null || orderMain.getUser() == null) {
            return ResultGenerator.genFailResult("参数错误");
        }
        userService.update(orderMain.getUser());
        return ResultGenerator.genSuccessResult(orderMain);
    }

    /**
     * 根据用户id获取订单列表
     */
    @GetMapping("/getByUser/{id}")
    public Result getOrderByUserId(@PathVariable("id") Integer userId){
        if(userId == null || userId == 0) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return orderMainService.getOrderByUserId(userId);
    }

    /**
     * 付款前检查
     */
    @PostMapping("/checkPay")
    public Result pay(@RequestBody OrderMain orderMain) {
        return orderMainService.checkPay(orderMain);
    }


    /**
     * 订单评价
     */
    @PostMapping("/comment")
    public Result receive(@RequestBody OrderComment comment) {
        if(comment == null) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return orderMainService.comment(comment);
    }

    /**
     * 订单收货
     * @param orderId
     */
    @PostMapping("/receive")
    public Result receive(@RequestBody Integer orderId) {
        if(orderId == 0 || orderId == null) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return orderMainService.receive(orderId);
    }

    /**
     * 管理平台后端接口部分
     */
    @GetMapping("/admin/num")
    public Result num(Integer id) {
        return orderMainService.num(id);
    }

    @PostMapping("/admin/list")
    public Result list(@RequestBody QueryParams queryParams) {
        if(queryParams == null) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return orderMainService.list(queryParams);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer orderId) {
        if(orderId == null || orderId == 0) {
            return ResultGenerator.genFailResult("参数错误");
        }
        OrderMain entity = orderMainService.findBy("orderId", orderId);
        if(entity == null) {
            return ResultGenerator.genFailResult("参数错误");
        }
        entity.setStatus(0);
        orderMainService.update(entity);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody OrderMain orderMain) {
        orderMainService.update(orderMain);
        return ResultGenerator.genSuccessResult(orderMain);
    }

    @GetMapping("/detail/{orderId}")
    public Result detail(@PathVariable("orderId") Integer orderId) {
        if(orderId == null || orderId == 0) {
            return ResultGenerator.genFailResult("参数错误");
        }
        return orderMainService.detail(orderId);
    }

    @GetMapping("/all")
    public Result all(){
        List<OrderMain> list = orderMainService.getList(null);
        return ResultGenerator.genSuccessResult(list);
    }
    @GetMapping("/all/{id}")
    public Result all2(@PathVariable("id") Integer shopId){
        List<OrderMain> list = orderMainService.getList(shopId);
        return ResultGenerator.genSuccessResult(list);
    }


    @GetMapping("/check/{id}")
    public Result check(@PathVariable("id") Integer shopId) {
        String msg = (String) redisTemplate.opsForValue().get(shopId.toString());
        if(msg == null) {
            return ResultGenerator.genFailResult("没有订单");
        }
        if(msg.equals("pay")) {
            redisTemplate.delete(shopId.toString());
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genFailResult("没有订单");
    }
}
